package shuashuami.hb.com.http;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by hufangda on 2017/7/26.
 */

public class UploadErrorHttp {

    private Activity activity;

    public UploadErrorHttp(Activity activity) {
        this.activity = activity;
    }

    public void upload(String content) {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                //   openActivityAndCloseThis(HbRegistered2Act.class);
                Message message = handler.obtainMessage(3);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);
            }
        };

        HttpMethods.uploadError(listener,
                content
        );
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {


                case 3:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(activity);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                     //       ToastUtil.showShort(activity, jsonObject.getString("message"));
                        } else {
                    //        ToastUtil.showShort(activity, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }

            super.handleMessage(msg);
        }
    };
}
